package ai;

import de.dqi.pong.engine.AI;
import de.dqi.pong.engine.Tick;

import static de.dqi.pong.engine.Direction.*;

/**
 * TODO Derp - finish class definition
 * <hr>
 * 
 * <hr><dl><dt>
 * Summary: </dt><br><dd>
 *  
 * </dd><br><dt>
 * Author: </dt><br><dd>
 *  dqi12lehmann
 * </dd></dl>
 */
public class Derp
		extends AI {
	
	private boolean	running;
	private long	currentTime;
	private long	lastTime;
	private double	y;
	private int		fps	= Tick.fps;
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		running = true;
		while (running) {
			currentTime = System.currentTimeMillis();
			
			
			if (tick != null) {
				if (isLeft) {
					y = tick.posBatLeft;
				} else {
					y = tick.posBatRight;
				}
				
				if (tick.posBallY > y + init.batSpan / 3) {
					direction = Up;
				} else if (tick.posBallY < y - init.batSpan / 3) {
					direction = Down;
				} else {
					direction = Hold;
				}
			}
			
			try {
				long timeout = (1000 / fps) - (System.currentTimeMillis() - currentTime);
				if (timeout > 0) {
					Thread.sleep(timeout);
				}
			} catch (InterruptedException e) {
				System.err.println("Wait interval interrupted");
			}
			lastTime = currentTime;
		}
		
	}
}
